package com.xh.imageeditor.sdk.core.file;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.xh.imageeditor.sdk.core.util.FileChooseUtils;

import java.io.File;

/**
 * Created by felix on 2017/12/26 下午3:07.
 */

public class IMGFileDecoder extends IMGDecoder {

    private Context mContext;

    public IMGFileDecoder(Context context, Uri uri) {
        super(uri);
        mContext = context;
    }

    @Override
    public Bitmap decode(BitmapFactory.Options options) {
        Uri uri = getUri();
        if (uri == null) {
            Log.e("ImageEditorSDK", "IMGFileDecoder decode() uri=null");
            return null;
        }

//        String path = uri.getPath();
        String path = FileChooseUtils.getInstance(mContext).getChooseFileResultPath(uri);
        if (TextUtils.isEmpty(path)) {
            Log.e("ImageEditorSDK", "IMGFileDecoder decode() path is empty, uri="+uri);
            return null;
        }

        File file = new File(path);
        if (file.exists()) {
            return BitmapFactory.decodeFile(path, options);
        }
        Log.e("ImageEditorSDK", "IMGFileDecoder decode() file not exist, path="+path);
        return null;
    }
}
